﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.Enums;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.Web;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Insert form for a new question
    /// </summary>
    public partial class InsertQuestion : PageBase
    {

        public override string ActionCode
        {
            get
            {
                return "100";//新建调查报告
            }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            MessageLabel.Visible = false;

            _displayOrder =
                    Information.IsNumeric(Request["DisplayOrder"]) && int.Parse(Request["DisplayOrder"]) > 0 ? int.Parse(Request["DisplayOrder"]) : 1;

            _pageNumber =
                Information.IsNumeric(Request["Page"]) && int.Parse(Request["Page"]) > 0 ? int.Parse(Request["Page"]) : 1;

            _libraryId =
                Information.IsNumeric(Request["LibraryId"]) ? int.Parse(Request["LibraryId"]) : -1;

            LocalizePage();

            if (!Page.IsPostBack)
            {
              //  Header.SurveyId = SurveyId;
            }
        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessFormBuilder, true);
            ImportXmlQuestionPlaceHolder.Visible = CheckRight(NSurveyRights.AllowXmlImport, false);
        }

        private void LocalizePage()
        {
            if (_libraryId != -1)
            {
                AddQuestionButton.Text = GetPageResource("AddQuestionToLibraryButton");

                LibraryData library = new Libraries().GetLibraryById(_libraryId);
                if (library.Libraries.Rows.Count != 0)
                {
                    NewQuestionTitle.Text = string.Format(
                        GetPageResource("NewLibraryQuestionTitle"), library.Libraries[0].LibraryName);
                }
            }
            else
            {
                NewQuestionTitle.Text = GetPageResource("NewQuestionTitle");
                AddQuestionButton.Text = GetPageResource("AddQuestionButton");
            }

            QuestionLabel.Text = GetPageResource("QuestionLabel");
            if (!Page.IsPostBack)
            {
                QuestionDropDownList.Items.Add(new ListItem(GetPageResource("SingleQuestionOption"), "single"));
               // QuestionDropDownList.Items.Add(new ListItem(GetPageResource("MatrixQuestionOption"), "matrix"));
                QuestionDropDownList.Items.Add(new ListItem(GetPageResource("StaticTextQuestionOption"), "static"));

                SourceDropDownList.Items.Add(new ListItem(GetPageResource("SelectSourceMessage"), "-1"));
                if (CheckRight(NSurveyRights.CopyQuestionFromAllSurvey, false) ||
                    NSurveyUser.Identity.HasAllSurveyAccess)
                {
                    SourceDropDownList.Items.Add(new ListItem(GetPageResource("SurveyOption"), "1"));
                }
            }
            ImportQuestionTitle.Text = GetPageResource("ImportQuestionTitle");
            ImportXMLButton.Text = GetPageResource("ImportXMLButton");
            TypeLabel.Text = GetPageResource("TypeLabel");
            if (_libraryId == -1)
            {
                CopyExistingQuestionButton.Text = GetPageResource("CopyExistingQuestionButton");
            }
            else
            {
                CopyExistingQuestionButton.Text = GetPageResource("CopyExistingQuestionToLibraryButton");
            }

            CopyExistingQuestionTitle.Text = GetPageResource("CopyExistingQuestionTitle");
            TypeLabel.Text = GetPageResource("TypeLabel");

        }

  
        protected void AddQuestionButton_Click(object sender, System.EventArgs e)
        {
            // Question cannot be empty
            if (QuestionFreeTextBox.Text.Length == 0)
            {
                MessageLabel.Visible = true;
                MessageLabel.Text = GetPageResource("MissingQuestionMessage");
            }
            else
            {
                if (_libraryId == -1)
                {
                    AddQuestion();
                }
                else
                {
                    AddQuestionToLibrary();
                }
            }

        }

        private void AddQuestion()
        {
            if (QuestionDropDownList.SelectedValue == "single")
            {
                // Adds the new single question
                QuestionData newQuestion =
                    new Question().AddDefaultSingleQuestion(SurveyId, _displayOrder, _pageNumber, QuestionFreeTextBox.Text);

                UINavigator.NavigateToSingleQuestionEdit(SurveyId, newQuestion.Questions[0].QuestionId, _libraryId, MenuIndex);
            }
            else if (QuestionDropDownList.SelectedValue == "matrix")
            {
                // Adds the new parent matrix question 
                QuestionData newQuestion =
                    new Question().AddDefaultMatrixQuestion(SurveyId, _displayOrder, _pageNumber, QuestionFreeTextBox.Text);

                UINavigator.NavigateToMatrixQuestionEdit(SurveyId, newQuestion.Questions[0].QuestionId, _libraryId, MenuIndex);
            }
            else
            {
                new Question().AddStaticInformationText(SurveyId, _displayOrder, _pageNumber, QuestionFreeTextBox.Text);
                UINavigator.NavigateToSurveyBuilder(SurveyId, MenuIndex);
            }
        }

        private void AddQuestionToLibrary()
        {
            if (QuestionDropDownList.SelectedValue == "single")
            {
                // Adds the new single question to tthe library 
                QuestionData newQuestion =
                    new Question().AddDefaultSingleQuestion(_libraryId, QuestionFreeTextBox.Text);

                UINavigator.NavigateToSingleQuestionEdit(SurveyId, newQuestion.Questions[0].QuestionId, _libraryId, MenuIndex);
            }
            else if (QuestionDropDownList.SelectedValue == "matrix")
            {
                // Adds the new parent matrix question to the library
                QuestionData newQuestion =
                    new Question().AddDefaultMatrixQuestion(_libraryId, QuestionFreeTextBox.Text);

                UINavigator.NavigateToMatrixQuestionEdit(SurveyId, newQuestion.Questions[0].QuestionId, _libraryId, MenuIndex);

            }
            else
            {
                new Question().AddStaticInformationText(_libraryId, QuestionFreeTextBox.Text);
                UINavigator.NavigateToLibraryTemplates(SurveyId, _libraryId, MenuIndex);
            }
        }

        protected void CopyExistingQuestionButton_Click(object sender, System.EventArgs e)
        {
            if (_libraryId == -1)
            {
                int questionId;
                if (SourceDropDownList.SelectedValue == "0")
                {
                    questionId = int.Parse(LibraryQuestionsDropDownList.SelectedValue);
                }
                else
                {
                    questionId = int.Parse(SurveyQuestionListDropdownlist.SelectedValue);
                }


                new Question().CopyQuestionById(questionId,
                    SurveyId, _displayOrder, _pageNumber);

                UINavigator.NavigateToSurveyBuilder(SurveyId, MenuIndex);
            }
            else
            {
                new Question().CopyQuestionToLibrary(int.Parse(SurveyQuestionListDropdownlist.SelectedValue),
                    _libraryId);
                UINavigator.NavigateToLibraryTemplates(SurveyId, _libraryId, MenuIndex);
            }
        }

        protected void SurveyListDropdownlist_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (SurveyListDropdownlist.SelectedValue == "-1")
            {
                SurveyQuestionListDropdownlist.Visible = false;
                CopyExistingQuestionButton.Enabled = false;
                SurveyQuestionListDropdownlist.Items.Clear();
            }
            else
            {
                SurveyQuestionListDropdownlist.Visible = true;
                QuestionData questions = new Questions().GetAnswerableQuestionListWithoutChilds(int.Parse(SurveyListDropdownlist.SelectedValue));
                if (questions.Questions.Rows.Count > 0)
                {
                    SurveyQuestionListDropdownlist.DataSource = questions;
                    SurveyQuestionListDropdownlist.DataMember = "questions";
                    SurveyQuestionListDropdownlist.DataTextField = "questionText";
                    SurveyQuestionListDropdownlist.DataValueField = "questionid";
                    SurveyQuestionListDropdownlist.DataBind();
                    CopyExistingQuestionButton.Enabled = true;
                }
                else
                {
                    SurveyQuestionListDropdownlist.Items.Clear();
                    SurveyQuestionListDropdownlist.Items.Insert(0, new ListItem(GetPageResource("NoQuestionToCopyAvailableMessage"), "-1"));
                }
            }
        }

        protected void ImportXMLButton_Click(object sender, System.EventArgs e)
        {
            if (ImportFile.PostedFile != null)
            {
                NSurveyQuestion importedQuestions = new NSurveyQuestion();
                try
                {
                    importedQuestions.ReadXml(ImportFile.PostedFile.InputStream);
                    SetImportedQuestionsDefaults(importedQuestions);
                    new Question().ImportQuestions(importedQuestions, NSurveyUser.Identity.UserId);
                    if (_libraryId == -1)
                    {
                        UINavigator.NavigateToSurveyBuilder(SurveyId, MenuIndex);
                    }
                    else
                    {
                        UINavigator.NavigateToLibraryTemplates(SurveyId, _libraryId, MenuIndex);
                    }
                }
                catch (Exception ex)
                {
                    MessageLabel.Text = ex.Message;
                    MessageLabel.Visible = true;
                }
            }
        }

        /// <summary>
        /// Set the imported questions local values (SurveyId, libraryid etc...)
        /// </summary>
        private void SetImportedQuestionsDefaults(NSurveyQuestion importedQuestions)
        {
            int questionDisplayOrder = _displayOrder;
            foreach (NSurveyQuestion.QuestionRow question in importedQuestions.Question)
            {
                if (_libraryId == -1)
                {
                    question.SurveyId = SurveyId;
                    question.PageNumber = _pageNumber;
                    question.DisplayOrder = questionDisplayOrder;
                    question.SetLibraryIdNull();
                }
                else
                {
                    question.SetSurveyIdNull();
                    question.PageNumber = 1;
                    question.DisplayOrder = 1;
                    question.LibraryId = _libraryId;
                }

                questionDisplayOrder++;
            }
        }

        protected void SourceDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (SourceDropDownList.SelectedValue == "-1")
            {
                SurveyListDropdownlist.Items.Clear();
                SurveyListDropdownlist.Visible = false;
                SurveyQuestionListDropdownlist.Items.Clear();
                SurveyQuestionListDropdownlist.Visible = false;
                LibraryDropDownList.Items.Clear();
                LibraryDropDownList.Visible = false;
                LibraryQuestionsDropDownList.Items.Clear();
                LibraryQuestionsDropDownList.Visible = false;
                CopyExistingQuestionButton.Enabled = false;

            }
            else if (SourceDropDownList.SelectedValue == "0")
            {
                SurveyListDropdownlist.Items.Clear();
                SurveyListDropdownlist.Visible = false;
                SurveyQuestionListDropdownlist.Items.Clear();
                SurveyQuestionListDropdownlist.Visible = false;

                // Retrieve the library data
                LibraryData libraryData =
                    new Libraries().GetLibraries();
                LibraryDropDownList.DataSource = libraryData;
                LibraryDropDownList.DataMember = "Libraries";
                LibraryDropDownList.DataTextField = "LibraryName";
                LibraryDropDownList.DataValueField = "LibraryId";
                LibraryDropDownList.DataBind();
                LibraryDropDownList.Items.Insert(0, new ListItem(GetPageResource("SelectLibraryMessage"), "-1"));
                LibraryDropDownList.Visible = true;
                CopyExistingQuestionButton.Enabled = false;
            }
            else
            {
                LibraryDropDownList.Items.Clear();
                LibraryDropDownList.Visible = false;
                LibraryQuestionsDropDownList.Items.Clear();
                LibraryQuestionsDropDownList.Visible = false;

                SurveyData surveys;
                if (CheckRight(NSurveyRights.CopyQuestionFromAllSurvey, false) ||
                    NSurveyUser.Identity.HasAllSurveyAccess)
                {
                    surveys = new Surveys().GetAllSurveysList();
                }
                else
                {
                    surveys = new Surveys().GetAssignedSurveysList(NSurveyUser.Identity.UserId);
                }

                SurveyListDropdownlist.DataSource = surveys;
                SurveyListDropdownlist.DataMember = "surveys";
                SurveyListDropdownlist.DataTextField = "title";
                SurveyListDropdownlist.DataValueField = "surveyid";
                SurveyListDropdownlist.DataBind();
                SurveyListDropdownlist.Items.Insert(0, new ListItem(GetPageResource("SelectSurveyMessage"), "-1"));
                SurveyListDropdownlist.Visible = true;
                CopyExistingQuestionButton.Enabled = false;
            }

        }

        protected void LibraryDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (SurveyListDropdownlist.SelectedValue == "-1")
            {
                LibraryQuestionsDropDownList.Visible = false;
                LibraryQuestionsDropDownList.Items.Clear();
                CopyExistingQuestionButton.Enabled = false;
            }
            else
            {
                LibraryQuestionsDropDownList.Visible = true;

                QuestionData questions = new Questions().GetLibraryQuestionListWithoutChilds(int.Parse(LibraryDropDownList.SelectedValue));
                if (questions.Questions.Rows.Count > 0)
                {
                    LibraryQuestionsDropDownList.DataSource = questions;
                    LibraryQuestionsDropDownList.DataMember = "questions";
                    LibraryQuestionsDropDownList.DataTextField = "questionText";
                    LibraryQuestionsDropDownList.DataValueField = "questionid";
                    LibraryQuestionsDropDownList.DataBind();
                    CopyExistingQuestionButton.Enabled = true;
                }
                else
                {
                    LibraryQuestionsDropDownList.Items.Clear();
                    LibraryQuestionsDropDownList.Items.Insert(0, new ListItem(GetPageResource("NoQuestionToCopyAvailableMessage"), "-1"));
                }
            }
        }

        private int _displayOrder = 1,
            _pageNumber = 1,
            _libraryId = -1;
    }

}
